var prefix = ctx + "module/pickUpPlan/";
var form, table, laydate,matterIdSelect;
$(function () {
    layui.use(['form', 'table', 'laydate', 'xmSelect'], function () {
        form = layui.form;
        table = layui.table;
        laydate = layui.laydate;
        form.on('select(typeId)', function (data) {
            typeIdChange(data.value, form)
        });
        laydate.render({
            elem: '#planDate'
        });
        lay('.time-select').each(function () {
            laydate.render({
                trigger: 'click',
                elem: this
                , type: 'time'
                , range: true
            });
        })


        $.ajax({
            url : ctx + "module/pickUpMatter/getMatterList",
            type: "post",
            success: function (r) {
                if (r.code == 0 && r.data.length > 0) {
                    var initValue = [];
                    if ($('#preMatterIds').val()) {
                        initValue = $('#preMatterIds').val().split(",")
                    }
                    var list = []
                    for (var i = 0; i < r.data.length; i++) {
                        var matter = r.data[i];
                        list.push({name: matter.matterName, value: matter.id})
                    }
                    matterIdSelect = xmSelect.render({
                        el: '#matterIds',
                        initValue: initValue,
                        data: list
                    })
                }else{
                    matterIdSelect = xmSelect.render({
                        el: '#matterIds',
                        data: []
                    })
                }
            }
        });

        if ($('.info-div').length == 0) {
            addInfoDiv();
        }
    })
});

$().ready(function () {
    validateRule();
});
$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});


function addInfoDiv() {
    $('#add_div').before('<div class="layui-form-item info-div" >\n' +
        '                    <label class="layui-form-label">计划：</label>\n' +
        '                    <div class="layui-input-block">\n' +
        '                        <label class="layui-form-label">选择时间：</label>\n' +
        '                        <div class="layui-input-block">\n' +
        '                            <input  type="text" autocomplete="off" class="layui-input time-select infoTimeRange"  placeholder=" - ">\n' +
        '                        </div>\n' +
        '                    </div>\n' +
        '                </div>');
    lay('.time-select').each(function () {
        laydate.render({
            trigger: 'click',
            elem: this
            , type: 'time'
            , range: true
        });
    })
}

function update() {
    var data = {}
    var pickUpPlanInfoList = []
    var matterIds = matterIdSelect.getValue('value').join(',');
    var matterNames = matterIdSelect.getValue('name').join(',');
    if(!matterIds){
        return parent.layer.msg("请选择物料!");
    }
    data.matterIds = matterIds;
    data.matterNames = matterNames;
    $('.info-div').each(function () {
        var pickUpPlanInfo = {}
        var infoTimeRange = $(this).find('.infoTimeRange').val()
        if (infoTimeRange) {
            pickUpPlanInfo.beginTime = infoTimeRange.split(" - ")[0]
            pickUpPlanInfo.endTime = infoTimeRange.split(" - ")[1]
            pickUpPlanInfoList.push(pickUpPlanInfo)
        }
    })
    $('#signupForm').serializeArray().forEach(function (res) {
        if (res.name != 'select') {
            data[res.name] = res.value
        }
    });
    data.pickUpPlanInfoList = pickUpPlanInfoList
    var url = prefix + ($('#signupForm').find("#id").val() == '' ? "save" : "update");
    $.ajax({
        type: "POST",
        url: url,
        contentType:"application/json;charset=UTF-8",
        data: JSON.stringify( data),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });
}


function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            planName: {
                required: true
            },
            planDate: {
                required: true
            },
        },
        messages: {
            planName: {
                required: icon + "请输入名称"
            },
            planDate: {
                required: icon + "请选择计划执行日期"
            },
        }
    })
}